home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Documents / develop GX articles & columns / Dev GX Extensions code ƒ / 4-Up.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  4.3 KB  |  173 lines  |  [TEXT/MPS ]

  1. /* ------------------------------------------------------------------------------
  2.  
  3.     FILENAME
  4.         4-Up.c
  5.  
  6.     DESCRIPTION
  7.         This file contains the code for a Printing Extension which does 4-up printing.
  8.  
  9.     COPYRIGHT
  10.         Copyright Apple Computer, Inc. 1993
  11.         All rights reserved. 
  12.     
  13.     MODIFICATION HISTORY
  14.         04/7/93            Sam Weiss            Initial Implementation
  15.  
  16.  
  17. ------------------------------------------------------------------------------- */
  18.  
  19.  
  20. #include <Types.h>
  21. #include <Exceptions.h>
  22. #include    <PrintingManager.h>
  23. #include    <PrintingMessages.h>
  24. #include <graphics routines.h>
  25. #include <math routines.h>
  26.  
  27.  
  28. // ------------------------------------------------------------------------------
  29.  
  30.  
  31. OSErr FourUpCountPages ( gxSpoolFile thePrintFile, long* numPages ) {
  32.  
  33.     OSErr anErr;
  34.     long originalPages;
  35.  
  36.     anErr = Forward_GXCountPages(thePrintFile, &originalPages);
  37.     nrequire (anErr, FailedForward_GXCountPages);
  38.  
  39.     *numPages = (originalPages + 3) / 4;
  40.  
  41. FailedForward_GXCountPages:
  42.     return anErr;
  43. }
  44.  
  45.  
  46. // ------------------------------------------------------------------------------
  47.  
  48.  
  49. OSErr FourUpDespoolPage (    gxSpoolFile thePrintFile, long pageNumber,
  50.                                     gxFormat pageFormat, gxShape* pagePicture,
  51.                                     Boolean* formatChanged    ) {
  52.     
  53.     OSErr anErr;
  54.     long firstPage, lastPage, numPages, whichPage;
  55.     gxShape fourUpPage;
  56.     gxShape thePages[4];
  57.     gxShape* atPage;
  58.     
  59.     // determine actual page number of pages to despool
  60.     
  61.     lastPage = pageNumber * 4;
  62.     firstPage = lastPage - 3;
  63.  
  64.     // determine page number for last page in spool file,
  65.     // so we can constrain our despooling loop to a valid range
  66.     // if less than four pages remain in the file
  67.     
  68.     anErr = ForwardMessage(gxCountPages, thePrintFile, &numPages);
  69.     nrequire (anErr, FailedForward_GXCountPages);
  70.     
  71.     if (lastPage > numPages)
  72.         lastPage = numPages;
  73.     
  74.     // create picture shape to hold sub-pages
  75.     
  76.     fourUpPage = GXNewShape(gxPictureType);
  77.     anErr = GXGetGraphicsError(nil);
  78.     nrequire (anErr, FailedGXNewShape);
  79.     
  80.     // despool backwards so pageFormat ends up containing
  81.     // the format for the first page in the group
  82.     
  83.     atPage = &thePages[lastPage-firstPage];        // point to last page in group
  84.     numPages = 0;                                            // track number of successfully despooled pages
  85.     
  86.     for (whichPage = lastPage; whichPage >= firstPage; --whichPage) {
  87.         anErr = Forward_GXDespoolPage (thePrintFile, whichPage, pageFormat, atPage--, formatChanged);
  88.         nrequire (anErr, FailedForward_GXDespoolPage);
  89.         ++numPages;
  90.     }
  91.     
  92.     // map and translate the despooled pages onto a single physical page
  93.     
  94.     {
  95.         gxRectangle pageRect;
  96.         fixed tx, ty;
  97.         gxMapping aMapping;
  98.         
  99.         // get the dimensions of the physical page
  100.         
  101.         GXGetFormatDimensions(pageFormat, &pageRect, nil);
  102.         
  103.         // compute x and y translation factors
  104.         
  105.         tx = (pageRect.right - pageRect.left) >> 1;
  106.         ty = (pageRect.bottom - pageRect.top) >> 1;
  107.         
  108.         // initialize the mapping
  109.         
  110.         ResetMapping(&aMapping);
  111.         aMapping.map[0][0] = fixed1/2;
  112.         aMapping.map[1][1] = fixed1/2;
  113.  
  114.         // map the pages onto the physical page
  115.         
  116.         GXMapShape(thePages[0], &aMapping);
  117.         
  118.         if (numPages > 1) {
  119.             MoveMapping(&aMapping, tx, 0);
  120.             GXMapShape(thePages[1], &aMapping);
  121.             if (numPages > 2) {
  122.                 MoveMapping(&aMapping, -tx, ty);
  123.                 GXMapShape(thePages[2], &aMapping);
  124.                 if (numPages > 3) {
  125.                     MoveMapping(&aMapping, tx, 0);
  126.                     GXMapShape(thePages[3], &aMapping);
  127.                 }
  128.             }
  129.         }
  130.         
  131.         // place the mapped pages into a single picture
  132.         
  133.         GXSetPictureParts(fourUpPage, 1, 0, numPages, thePages, nil, nil, nil);
  134.         anErr = GXGetGraphicsError(nil);
  135.         nrequire (anErr, FailedGXSetPictureParts);
  136.  
  137.         // GXSetPictureParts cloned the pages,
  138.         // so we must dispose our references to them
  139.         
  140.         for (atPage = &thePages[numPages-1]; atPage >= thePages; --atPage)
  141.             GXDisposeShape(*atPage);
  142.     }
  143.     
  144.     // return the 4-up page
  145.     
  146.     *pagePicture = fourUpPage;
  147.     
  148.     // since we don't know whether the format for
  149.     // "actual page number 5" is the same as that for
  150.     // "actual page number 1", we always set formatChanged to true
  151.             
  152.     *formatChanged = true;
  153.     
  154.     ncheck (anErr);
  155.     return noErr;
  156.  
  157. //------------------------
  158. // exception handling code
  159. //------------------------
  160.  
  161. FailedGXSetPictureParts:
  162. FailedForward_GXDespoolPage:
  163.     for (atPage = &thePages[numPages-1]; atPage >= thePages; --atPage)
  164.         GXDisposeShape(*atPage);
  165.     GXDisposeShape(fourUpPage);
  166. FailedGXNewShape:
  167. FailedForward_GXCountPages:
  168.     return anErr;
  169. }
  170.  
  171.  
  172. // ------------------------------------------------------------------------------
  173.